<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?
	/********************************************************************************
	 Configuration
	 *********************************************************************************/ 


	// starting folder name (highest level that should be visible)  
	$location = "multimedia";
	
	// enter file extentions for files that should be displayed, including the "." -- i.e. array('.doc',".htm')
	$file_ext =  array('.mp4','.m4v','.mp3','.jpg','.m4a');
	
	// Stuff to be ignored...
	//Ignore the file/folder if these words appear in the name (wildcards (*) accepted)
	$always_ignore = array('.', '..', 'index.php');		
	
	/*
	 This optional field may be changed to sort file names in numerical order
	 Sorting type flags:
		* NUMERIC - compare items numerically
		* STRING - compare items as strings 
	 */
	$sortflag = "STRING";
	
	$back = "";
	$next = "";
	
	$filenames;
	
	function Onload()
	{
		global $HTTP_SERVER_VARS, $location, $file, $sortflag, $back, $next, $filenames;
		
		// if location past through url, define location       
		if(isset($_GET['loc'])) 
			$location = htmlspecialchars($_GET['loc']);

		// if file past through url, define filename  
		if(isset($_GET['file'])) 
			$file = htmlspecialchars($_GET['file']);

		//  Open  the  folder
		$dir_handle  =  @opendir($location)  or  die("Unable  to  open  path ".$path);

		//  Loop  through  the  files
		while (false !== ($name = readdir($dir_handle)))
			$filenames[] = $name;
		
		//sort links
		if($sortflag == "NUMERIC")
			array_multisort($filenames, SORT_ASC, SORT_NUMERIC);
		else
			array_multisort($filenames, SORT_ASC, SORT_REGULAR); 	

		//  Close
		closedir($dir_handle);
		
		if($file != "")
		{
			$count = count($filenames);
			for ($i = 0; $i <= $count; $i++)
			{
				if($filenames[$i] == $file)
				{
					$current = $filenames[$i];
					if($i > 0)
					{
						$back = $filenames[$i-1];
					}
					else
						$back = $current;
						
					if($i < $count)
					{
						$next = $filenames[$i+1];
					}
					else
						$next = $current;
						
					$ext = pathinfo($back, PATHINFO_EXTENSION);
					if($ext != "JPG" and $ext != "jpg")
						$back = $current;
					
					$ext = pathinfo($next, PATHINFO_EXTENSION);
					if($ext != "JPG" and $ext != "jpg")
						$next = $current;
				}
			}			
		}		
	}

	/*     To Additional customization is required 
	 *******************************************************************************
	 DisplayContent - displays a link to all the files in a folder at the location  
	                specified and with the file extensions listed above
	 *********************************************************************************/ 
	function DisplayContent()
	{ 
		global $HTTP_SERVER_VARS, $location, $file, $filenames;

		if($file == "")
		{
			echo "<ul>";
			foreach($filenames as $key => $name)
			{
				if(my_inArray($name,$GLOBALS['always_ignore']))
					continue;
				
				// clean link text for display
				$link = CleanLink($name);
				
				// format link differently for folder names
				if(!eregi(".*\.",$name))
				{
					if($name != "thumbs")
					{
						echo "<li><a class=\"noeffect\" href=\"?loc=$location/$name\"><span class=\"name\">$link</span><span class=\"arrow\"></a></li>\r\n";
					}
				}
				elseif(AuthorizedFile($name))
				{
				
					$ext = pathinfo($name, PATHINFO_EXTENSION);

					if($ext == "JPG" or $ext == "jpg")
					{
						if (!is_dir("$location/thumbs/"))
						{
							mkdir("$location/thumbs/");
						}
						
						if (!file_exists("$location/thumbs/$name"))
						{
							$maxheight = 150;
							$maxwidth = 150;
						
							// load image and get image size
							$img = imagecreatefromjpeg("$location/$name");
							$width=imageSX($img);
							$height=imageSY($img);
							
							if ($height < $width) 
							{   
								$ratio = $maxheight / $height;  
								$newheight = $maxheight;
								$newwidth = $width * $ratio; 
								$writex = round(($newwidth - $maxwidth) / 2);
								$writey = 0;
							}
							else 
							{
								$ratio = $maxwidth / $width;   
								$newwidth = $maxwidth;  
								$newheight = $height * $ratio;   
								$writex = 0;
								$writey = round(($newheight - $maxheight) / 2);
							}
							
							// create a new temporary image
							$newimg = imagecreatetruecolor($maxwidth, $maxheight);
							
							// copy and resize old image into new image
							imagecopyresized($newimg, $img, 0, 0, $writex, $writey, $newwidth, $newheight, $width, $height);
							
							// save thumbnail into a file
							imagejpeg( $newimg, "$location/thumbs/$name" );
						}

						echo "<li class=\"withimage\">\r\n";
						echo "<a class=\"noeffect\" href=\"?loc=$location&file=$name\">";
						echo "<img src=\"$location/thumbs/$name\" /><span class=\"name\">$link</span></a>\r\n";
						echo "</li>\r\n";					
					}
					else
					{
						echo "<li><a class=\"noeffect\" href=\"$location/$name\"><span class=\"name\">$link</span></a></li>\r\n";
					}
				}	
			}
			echo "</ul>";
		}
		else
		{		
			echo "<a href=\"$location/$file\" ><img width=\"100%\" src=\"$location/$file\" /></a>";
		}	
	}
			
	/********************************************************************************
	 my_inArray checks for ignored values
	 *********************************************************************************/ 
	function my_inArray($needle, $haystack)
	{
		# this function allows wildcards in the array to be searched
		foreach ($haystack as $value)
		{
			if (true === fnmatch($value, $needle))
			{
				return true;
			}
		}
		return false;
	}
	
	/********************************************************************************
	 CleanLink formats link for display (removes special characters and file extentions)
	 *********************************************************************************/ 
	function CleanLink($link)
	{
		global $file_ext;		
		$link = ereg_replace('_', ' ', $link); 		
		foreach($file_ext as $key => $value)
			$link = ereg_replace($value, '', $link); 		
		
		$link = ucwords(strtolower($link));
		
		return $link;
	}
     
	/********************************************************************************
	 AuthorizedFile - checks array of authorized file extensions
	 *********************************************************************************/ 
	function AuthorizedFile($file)
	{
		global $file_ext;
	
		foreach($file_ext as $key => $value)
			if(eregi($value,$file)) return 1;

		return 0;	
	}

	function BackHRef()
	{
		global $location, $file;
		
		if($file == "")
		{
			$pathArray = split('/', $location);
			$count = count($pathArray);
			
			if($count > 1)
			{
				$path = "";		
				for ($i = 0; $i < $count-1; $i++)
				{
					if($i > 0)
						$path = "$path/";
					$path = "$path$pathArray[$i]";
				}		
				return $path;
			}
		}
		else
		{
			return $location;
		}	
	}
?> 

<html xmlns="http://www.w3.org/1999/xhtml">

<?php
	Onload();
?>

<head>
		
	<meta content="yes" name="apple-mobile-web-app-capable" />
	<meta content="index,follow" name="robots" />
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
	<meta content="iPod,iPhone,Webkit,iWebkit,Website,Create,mobile,Tutorial,free" name="Keywords" />
	<meta content="This special iTunes music list emulates the feeling of the itunes app on the iphone." name="description" />

	<meta content="minimum-scale=1.0, width=device-width, maximum-scale=0.6667, user-scalable=yes" name="viewport" />

	<link href="pics/homescreen.gif" rel="apple-touch-icon" />
	<link href="css/style.css" rel="stylesheet" media="screen" type="text/css" />

	<script src="javascript/functions.js" type="text/javascript"></script>

	<title>iBrowse</title>
	
<script type="text/javascript">

/*****************************************************

 TERMS OF USE

 You may freely use, distribute, and modify this code
 for any purpose, so long as you attribute the code as
 follows:

 Author:                              Matthew Congrove
 More Information:               mydailyphoto.com/blog

******************************************************/

/*****************************************************

 NOTICE:
 
 Vertical scrolling is disabled using the Touch API
 (Line 154: e.preventDefault). This means that it is
 impossible for a user to access the window location
 bar on the iPhone, so please be kind and provide some
 sort of 'Close' button, or modify the script to suit
 your needs, as well as those of your users.
 
 NOTICE:
 
 All debugging code is clearly marked as such. Just
 search for 'Debug'.
 
 MORE INFORMATION:
 
 For a complete tutorial on how this code works,
 please visit the myDailyPhoto blog entry --
 
 mydailyphoto.com/blog/iphone-safari-flick-navigation

******************************************************/

var touching, dPrevious, dCurrent, dNext, oX;

// Whether or not the finger is touching the screen
touching = false;

// Original X-coordinate
oX = 0;

// Initial page numbers
dPrevious = 0;
dCurrent = 1;
dNext = 2;

<?php	
// Apple iPhone Touch API events

global $file;
		
if($file != "")
{	
	echo "document.addEventListener('touchstart', touchHandler, false);";
	echo "document.addEventListener('touchmove', touchHandler, false);";
	echo "document.addEventListener('touchend', touchHandler, false);";
	echo "document.addEventListener('touchcancel', touchHandler, false);";
}
?>	

// The handler for all Apple iPhone Touch API events
function touchHandler(e)
{
	// If the user has started a touch event
	if (e.type == "touchstart")
	{
		touching = true;
		// If there's only one finger touching
		if (e.touches.length == 1)
		{
			var touch = e.touches[0];
			// If they user tries clicking on a link
			if(touch.target.onclick) {
				touch.target.onclick();
			}
			// The originating X-coord (point where finger first touched the screen)
			oX = touch.pageX;
			// Reset default values for current X-coord and scroll distance
			nX = 0;
			scrollX = 0;
		}
	}
	// If the user has touched the screen and moved the finger
	else if (e.type == "touchmove")
	{
		// Prevent the default scrolling behaviour (notice: This disables vertical scrolling as well)
//	       e.preventDefault();

		// If there's only one finger touching
		if (e.touches.length == 1)
		{
			var touch = e.touches[0];
			// The current X-coord of the users finger
			var nX = touch.pageX;
			
			// If the user moved the finger from the right to the left
			if (oX > nX)
			{
				// Find the scrolling distance
				var scrollX = oX-nX;
				// If the user scrolled more than 100 pixels
				if (scrollX > 100)
				{
					// If this is still from the original touch
					if (touching == true)
					{
						// End the current touch
						touching = false;
						// Move in the next DIV
						left();
					}
				}
			// If the user moved the finger from the left to the right
			}
			else
			{
				// Find the scrolling distance
				var scrollX = nX-oX;
				// If the user scrolled more than 100 pixels
				if (scrollX > 100)
				{
					// If this is still from the original touch
					if (touching == true) {
						// End the current touch
						touching = false;
						// Move in the previous DIV
						right();
					}
				}
			}
		}
	}
	// If the user has removed the finger from the screen
	else if (e.type == "touchend" || e.type == "touchcancel")
	{
		// Defines the finger as not touching
		touching = false;
	}
}
function right()
{
<?php	
	global $location, $file, $back, $next;
			
	if($file != "")
	{
		echo "window.location.href=\"?loc=$location&file=$back\";";
	}
?>	
}

function left()
{
<?php	
	global $location, $file, $back, $next;

	if($file != "")
	{
		echo "window.location.href=\"?loc=$location&file=$next\";";
	}
?>	
}
</script>	
	
</head>

<body class="list" >

<div id="topbar">
	<div id="leftnav">
		<a href="index.php?loc=multimedia"><img alt="home" src="images/home.png" /></a>
<?
	$href = BackHRef();
	if($href != "")
		echo "<a href=\"index.php?loc=$href\">Back</a>";
?> 			
	</div>
	
</div>

<?
/*
	global $location, $file, $back, $next;
			
	if($file != "")
	{
		$ext = pathinfo($file, PATHINFO_EXTENSION);
		if($ext == "JPG" or $ext == "jpg")
		{	
			echo "<div id=\"tributton\">";
			echo "<div class=\"links\">";
			echo "<a href=\"?loc=$location&file=$back\"><</a><a href=\"\">Slidshow</a><a href=\"?loc=$location&file=$next\">></a>";
			echo "</div>";
			echo "</div>";
		}
	}
*/
?>

<div id="content">
	<?php
		DisplayContent();
	?> 
</div>

<div id="footer">
	<a href="http://iwebkit.net">Powered by iWebKit</a>
</div>

<?php
	echo "<img width=\"0%\" src=\"$location/$back\" />\n";
	echo "<img width=\"0%\" src=\"$location/$next\" />";
?> 

</body>

</html>
